/**
 * 用于检查出牌是否合理
 * 用于检查是否和上一个玩家的牌模型相同
 */
var playerData = require('../scripts/network/playerData');

function checkCard () {
    

};


checkCard.prototype.check = function (cardArr,otherCardArr,sex) {
    var audio = cc.find('Canvas/audioNode').getComponent('audio');
    console.log('自己点的牌');
    console.log(cardArr);
    console.log('其他玩家出的牌');
    console.log(otherCardArr);
    var result;
    if(otherCardArr.length != 0) {
        if(otherCardArr.length == 1 && cardArr.length == 1) {
            if(cardArr[0][2] > otherCardArr[0][2]) {
                audio.playAudio(sex, cardArr[0][2]);
                result = 0;
            }else {
                result = 1;
            }
        }else if(otherCardArr.length == 2 && cardArr.length == 2) {
            if(cardArr[0][2] == cardArr[1][2] && cardArr[0][2] > otherCardArr[0][2]) {
                var num = cardArr[0][2] *10;
                audio.playAudio(sex, num);
                result = 0;
            }else {
                result = 1;
            }
        }else if(otherCardArr.length == 3 && cardArr.length == 3) {
            if(cardArr[0][2] == cardArr[1][2] && cardArr[0][2] == cardArr[2][2] && cardArr[0][2] > otherCardArr[0][2]) {
                audio.playAudio(sex, 300);
                result = 0;
            }else {
                result = 1;
            }
        }else if(otherCardArr.length == 4 && cardArr.length == 4) {
            var countO= 0,countA = 0,boom = 0;
            for (var i =1,k=0; i<otherCardArr.length; i++,k++) {
                if(otherCardArr[0][2] != otherCardArr[1][2] || otherCardArr[2][2] != otherCardArr[3][2]) {
                    if(cardArr[i-1][2] == cardArr[i][2]) {
                        countA ++ ;
                        if(cardArr[k][2] > otherCardArr[k][2]) {
                            countO ++ ;
                        }
                    }
                    
                }else {
                    if(cardArr[3][2] == cardArr[2][2] && cardArr[0][2] == cardArr[1][2] ) {
                        
                        if(cardArr[0][2] > otherCardArr[0][2]) {
                            boom =1;
                        }else {
                            boom =0;
                        }
                    }
                }
            }
            if(countA == 2) {       //三带一
                if(countO >= 2) {
                    audio.playAudio(sex, 301);
                    result = 0;
                }else {
                    result = 1;
                }
            }else if (countA == 3) {  //炸弹
                audio.playAudio(sex, 400);
                result = 0;
            }else if(boom == 1) {   //炸弹比上加大
                audio.playAudio(sex, 400);
                result = 0;
            }
            else {
                result = 1;
            }
        }else if (otherCardArr.length == 5 && cardArr.length == 5) {
            var countA =0, countO = 0, count =0, count2 =0;
            for (var i = 1; i< 5; i++) {
                if(cardArr[i-1][2] == cardArr[i][2]) {       //这里判断玩家点击的牌是否是 3带2
                    count++;
                }
                
                if(cardArr[i-1][2] > otherCardArr[i-1][2]) {  //这里判断玩家的牌是否大于上家
                    countA ++;
                }
                if(otherCardArr[i-1][2] == otherCardArr[i][2]) {      //这里判断上家的牌是否的是 3带2
                    countO++;
                }
                if((cardArr[i-1][2] -1) == cardArr[i][2]) {
                    if(cardArr[i-1][2] > otherCardArr[i-1][2]) {
                        count2 ++;
                    }
                }
                
            }
            if(countO == 3 && count == 3 && countA >= 3) {    //三代二
                audio.playAudio(sex, 302);
                result = 0;
            }else if (countO == 0 && count == 0 && count2 == 4){
                audio.playAudio(sex, 401);
                result = 0;
            }else {
                console.log(countO,count,countA,count2);
                result = 1;
            }
        }else if(cardArr.length == otherCardArr.length && otherCardArr.length >= 6) {
            var count = 0, count2 = 0, countA = 0;
            for(var i = 1; i< otherCardArr.length; i++) {
                if((otherCardArr[i-1][2] - 1) == otherCardArr[i][2]) {
                    if((cardArr[i-1][2] -1) == cardArr[i][2] && cardArr[i-1][2] > otherCardArr[i-1][2]) {
                        count ++;
                    }
                }
                if(otherCardArr[i-1][2] == otherCardArr[i][2]) {
                    count2++;
                }
                if(cardArr[i-1][2] == cardArr[i][2] && cardArr[4][2] > otherCardArr[4][2]) {
                    countA++;
                }
            }
            if(count == (cardArr.length -1)) {             //顺子
                audio.playAudio(sex, 401);
                result = 0;
            }else if (count2 == countA && countA == cardArr.length/2) {  //连队
                audio.playAudio(sex, 404); 
                result = 0;
            }else if(count2 == countA && cardArr.length == 8) {   //飞机带单
                audio.playAudio(sex, 403);
                result = 0;
            }else if(count2 == countA && cardArr.length == 10) {    //飞机带对子
                audio.playAudio(sex, 403);
                result = 0;
            }else {
                result = 1;
            }
        }else if (otherCardArr.length != 4 && cardArr.length == 4) {
            var temp = cardArr[0][2],count = 0;
            for(var i in cardArr) {
                if(temp == cardArr[i][2]) {
                    count ++;
                }            
            }
            if(count == 4) {
                result = 0;
            }else {
                result = 1;
            }
        }else if (otherCardArr.length == 2) {
            if(otherCardArr[0][2] == 17 && otherCardArr[1][2] == 16) {
                result = 1;
            }
            
        }else if(cardArr[0][2] == 17 && cardArr[1][2] == 16) {
            result = 0;
        }
        return result;

    }else {               //如果上家没出牌，或者自己先出牌
        if(cardArr.length == 1) {               //出一张牌
            var num = cardArr[0][2]
            audio.playAudio(sex, num);
            result = 0;
        }else if(cardArr.length == 2) {         //出两张牌
            if(cardArr[0][2] == cardArr[1][2]) {
                var num = cardArr[0][2] *10;
                audio.playAudio(sex, num);
                result = 0;
            }else {
                result = 1;
            }
            if(cardArr[0][2] == 17 && cardArr[1][2] == 16) {
                //audio.playAudio(sex, )
                result = 0;
            }
        }else if(cardArr.length == 3) {         //出三张牌
            if(cardArr[0][2] == cardArr[2][2] && cardArr[0][2] == cardArr[1][2]) {
                audio.playAudio(sex, 300);
                result = 0;
            }else {
                result = 1;
            }
        }else if(cardArr.length == 4) {         //出四张牌 
            var count = 0;
            for(var i = 1; i< cardArr.length; i++) {
               
                if(cardArr[0][2] != cardArr[1][2] || cardArr[2][2] != cardArr[3][2]) {
                    if(cardArr[i-1][2] == cardArr[i][2]) {
                    count ++;
                    }
                }
                
            }
            if(count == 2) {    //三带一
                audio.playAudio(sex, 301);
                result = 0;
            }else if(count == 3) {
                audio.playAudio(sex, 400);
                result = 0;
            }else {
                result = 1;
            }
        }else if(cardArr.length == 5) {         //出五张牌
            console.log(cardArr.length);
            var count = 0, count2 = 0;
            for(var i = 1; i < cardArr.length; i++) {
                
                if(cardArr[i-1][2] == cardArr[i][2]) {
                    count++;
                }
                if((cardArr[i-1][2] - 1) == cardArr[i][2]) {
                    count2 ++; 
                }
            }
            console.log(count,count2);
            if(count == 0) {          //顺子
                if(count2 == 4) {
                    audio.playAudio(sex, 401);
                    result = 0;
                }else {
                    result = 1;
                }
            }else if(count == 1) {
                result = 1;
            }else if(count ==2) {
                result = 1;
            }else if(count == 3) {  //三代二
                audio.playAudio(sex, 302);
                result = 0;
            }
        }else if(cardArr.length >= 6) {         //出六张牌
            var count = 0, count2 = 0;
            for(var i = 1; i<cardArr.length; i++) {
                
                if((cardArr[i-1][2] -1) == cardArr[i][2]) {
                    count++;
                }
                if(cardArr[i-1][2] == cardArr[i][2]) {
                    count2++;
                }
            }
            if(count == (cardArr.length -1)) {   //顺子
                audio.playAudio(sex, 401);
                result = 0;
            }else if(count2 == cardArr.length/2) {
                audio.playAudio(sex, 404);
                result = 0;
            }else if(count2 == 4 && cardArr.length == 6) {
                audio.playAudio(sex, 402);
                result =0;
            } else if(count2 == 4 && cardArr.length == 8){
                audio.playAudio(sex, 403);
                result = 0;
            }else if(count2 == 6 && cardArr.length == 10) {
                audio.playAudio(sex, 403);
                result = 0;
            }else {
                result =1;
            }

        }
    }
    return result;
}

module.exports = checkCard;